package com.jonsion.service.impl;

import java.util.List;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jonsion.dao.IAccountDao;
import com.jonsion.dao.mapper.AccountMapper;
import com.jonsion.domain.Account;
import com.jonsion.exception.CustomException;
import com.jonsion.service.IAccountService;
import com.jonsion.util.MD5;
import com.jonsion.util.StringUtil;
import com.jonsion.web.controller.AccountController;

@Service(value="accountService")
@Transactional
public class AccountServiceImpl implements IAccountService {
	protected static final Logger logger = LoggerFactory
	.getLogger(AccountServiceImpl.class);
	
	@Inject 
    private AccountMapper accountMapper; 
	
	@Autowired
	private IAccountDao accountDao;
	
	private final int PASSTIMES=10;//加密次数
	
	private final String befStr="[";
	
	private final String aftStr="]";
	
	@Transactional
	public Account regist(Account ac)  throws CustomException{
		// TODO Auto-generated method stub
		try {
			List<Account> ls=accountMapper.getAccountByUserName(ac);
			
			if(ls==null || ls.size()==0){
				
				Account tac=(Account)ac.clone();
				
				String newPassword = getNewPassword(tac);
				
				tac.setUserPassword(newPassword);
				
				accountMapper.add(tac);
				
			}else{
				
				throw  new CustomException("もう存在した");
				
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 加一下密
	 * @param tac
	 * @return
	 */
	private String getNewPassword(Account tac) {
		
		String newPassword="";
		
		String username=tac.getUserName();
		
		String password=tac.getUserPassword();
		
		String md5src=StringUtil.padAround(username, befStr, aftStr)+password;
		
		newPassword=MD5.getMD5ofStr(md5src, PASSTIMES);
		
		return newPassword;
	}

	public boolean login(Account ac)  throws CustomException{
		// TODO Auto-generated method stub
		boolean flag=false;
		try {
			Account tac=(Account)ac.clone();
			
			String newPassword = getNewPassword(tac);
			
			tac.setUserPassword(newPassword);
			
			List<Account> ls=accountMapper.getAccountByUserName(tac);
			
			if(ls==null || ls.size()==0){
				
				throw  new CustomException("存在しない");
				
			}else{
				
				for(Account tmpac : ls){
					if(tac.getUserPassword().equals(tmpac.getUserPassword())){
						logger.debug("registdate>>>"+tmpac.getRegistdate().toLocaleString());
						flag = true;
						break;
					}
				}
				
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw  new CustomException("ユーザを取る時にエラーが発生した",e);
		}
		
		return flag;
	}
	
	
	public void deleteAccount(Account ac)  throws CustomException{
		
		accountDao.delete(ac);
		
	}

}
